﻿#include "WCKZTest.hpp"
#include <uf_oper.h>
#include<uf_ui.h>
#include<uf_setup.h>
#include<uf_assem.h>
#include<uf_ncgroup.h>
#include<uf_obj.h>
#include<uf_param.h>
#include<uf_param_indices.h >
#include<uf_camgeom.h>
#include<uf_cut_levels.h>
#include<uf_ui_ont.h>

#include <uf_defs.h>
#include <NXOpen/NXException.hxx>
#include <NXOpen/Session.hxx>
#include <NXOpen/Annotations_Annotation.hxx>
#include <NXOpen/Annotations_AnnotationManager.hxx>
#include <NXOpen/Annotations_BreakSettingsBuilder.hxx>
#include <NXOpen/Annotations_DimensionStyleBuilder.hxx>
#include <NXOpen/Annotations_DisplayStyleBuilder.hxx>
#include <NXOpen/Annotations_DraftingNoteBuilder.hxx>
#include <NXOpen/Annotations_ForeshorteningSymbolBuilder.hxx>
#include <NXOpen/Annotations_ForeshorteningSymbolSettingsBuilder.hxx>
#include <NXOpen/Annotations_FrameBarElementStyleBuilder.hxx>
#include <NXOpen/Annotations_FrameBarStyleBuilder.hxx>
#include <NXOpen/Annotations_HatchStyleBuilder.hxx>
#include <NXOpen/Annotations_HoleCalloutSettingsBuilder.hxx>
#include <NXOpen/Annotations_LeaderBuilder.hxx>
#include <NXOpen/Annotations_LeaderData.hxx>
#include <NXOpen/Annotations_LeaderDataList.hxx>
#include <NXOpen/Annotations_LetteringStyleBuilder.hxx>
#include <NXOpen/Annotations_LineArrowStyleBuilder.hxx>
#include <NXOpen/Annotations_OrdinateStyleBuilder.hxx>
#include <NXOpen/Annotations_OriginBuilder.hxx>
#include <NXOpen/Annotations_PlaneBuilder.hxx>
#include <NXOpen/Annotations_RadialStyleBuilder.hxx>
#include <NXOpen/Annotations_SingleSidedDisplayBuilder.hxx>
#include <NXOpen/Annotations_StyleBuilder.hxx>
#include <NXOpen/Annotations_SymbolStyleBuilder.hxx>
#include <NXOpen/Annotations_TextWithEditControlsBuilder.hxx>
#include <NXOpen/Annotations_TextWithSymbolsBuilder.hxx>
#include <NXOpen/Annotations_UnitsStyleBuilder.hxx>
#include <NXOpen/BasePart.hxx>
#include <NXOpen/Builder.hxx>
#include <NXOpen/CAM_CAMSession.hxx>
#include <NXOpen/CAM_CAMSetup.hxx>
#include <NXOpen/CAM_CutVolumeGeom.hxx>
#include <NXOpen/CAM_Cycle.hxx>
#include <NXOpen/CAM_FeatureGeomBuilder.hxx>
#include <NXOpen/CAM_FeatureGeometry.hxx>
#include <NXOpen/CAM_GeometryCiBuilder.hxx>
#include <NXOpen/CAM_HoleBossGeom.hxx>
#include <NXOpen/CAM_InheritableDoubleBuilder.hxx>
#include <NXOpen/CAM_LayoutCiBuilder.hxx>
#include <NXOpen/CAM_MillTextGeomBuilder.hxx>
#include <NXOpen/CAM_MultiBladeBaseGeometry.hxx>
#include <NXOpen/CAM_MultiBladeSplittersGeometry.hxx>
#include <NXOpen/CAM_NCGroup.hxx>
#include <NXOpen/CAM_NCGroupBuilder.hxx>
#include <NXOpen/CAM_NCGroupCollection.hxx>
#include <NXOpen/CAM_OrientGeometry.hxx>
#include <NXOpen/CAM_ParamBuilder.hxx>
#include <NXOpen/CAM_PathDisplay.hxx>
#include <NXOpen/CAM_Splitter.hxx>
#include <NXOpen/CAM_SplitterList.hxx>
#include <NXOpen/CAM_SpunOutlineGeom.hxx>
#include <NXOpen/CAM_Ude.hxx>
#include <NXOpen/CAM_UdeList.hxx>
#include <NXOpen/CAM_UdeSet.hxx>
#include <NXOpen/DisplayableObject.hxx>
#include <NXOpen/Drawings_DraftingView.hxx>
#include <NXOpen/Drawings_SelectDraftingView.hxx>
#include <NXOpen/NXObject.hxx>
#include <NXOpen/ObjectList.hxx>
#include <NXOpen/Part.hxx>
#include <NXOpen/PartCollection.hxx>
#include <NXOpen/Plane.hxx>
#include <NXOpen/SIM_KinematicConfigurator.hxx>
#include <NXOpen/SelectDisplayableObject.hxx>
#include <NXOpen/SelectDisplayableObjectList.hxx>
#include <NXOpen/SelectObject.hxx>
#include <NXOpen/Session.hxx>
#include <NXOpen/TaggedObject.hxx>
#include <NXOpen/Xform.hxx>
#include<uf_drf.h>
using namespace NXOpen;
using namespace NXOpen::BlockStyler;

//------------------------------------------------------------------------------
// Initialize static variables
//------------------------------------------------------------------------------
Session *(WCKZTest::theSession) = NULL;
UI *(WCKZTest::theUI) = NULL;
//------------------------------------------------------------------------------
// Constructor for NX Styler class
//------------------------------------------------------------------------------
WCKZTest::WCKZTest()
{
    try
    {
        // Initialize the NX Open C++ API environment
        WCKZTest::theSession = NXOpen::Session::GetSession();
        WCKZTest::theUI = UI::GetUI();
        theDlxFileName = "E:\\Code\\UGDemo\\DX_KZ\\lesson15_NX10UI_creatBlock1\\lesson15_NX10UI_creatBlock1\\lesson15_NX10UI_creatBlock1\\x64\\WCKZTest.dlx";
        theDialog = WCKZTest::theUI->CreateDialog(theDlxFileName);
        // Registration of callback functions
        theDialog->AddApplyHandler(make_callback(this, &WCKZTest::apply_cb));
        theDialog->AddOkHandler(make_callback(this, &WCKZTest::ok_cb));
        theDialog->AddUpdateHandler(make_callback(this, &WCKZTest::update_cb));
        theDialog->AddInitializeHandler(make_callback(this, &WCKZTest::initialize_cb));
        theDialog->AddDialogShownHandler(make_callback(this, &WCKZTest::dialogShown_cb));
    }
    catch(exception& ex)
    {
        //---- Enter your exception handling code here -----
        throw;
    }
}

//------------------------------------------------------------------------------
// Destructor for NX Styler class
//------------------------------------------------------------------------------
WCKZTest::~WCKZTest()
{
    if (theDialog != NULL)
    {
        delete theDialog;
        theDialog = NULL;
    }
}
//------------------------------- DIALOG LAUNCHING ---------------------------------
//
//    Before invoking this application one needs to open any part/empty part in NX
//    because of the behavior of the blocks.
//
//    Make sure the dlx file is in one of the following locations:
//        1.) From where NX session is launched
//        2.) $UGII_USER_DIR/application
//        3.) For released applications, using UGII_CUSTOM_DIRECTORY_FILE is highly
//            recommended. This variable is set to a full directory path to a file 
//            containing a list of root directories for all custom applications.
//            e.g., UGII_CUSTOM_DIRECTORY_FILE=$UGII_BASE_DIR\ugii\menus\custom_dirs.dat
//
//    You can create the dialog using one of the following way:
//
//    1. USER EXIT
//
//        1) Create the Shared Library -- Refer "Block UI Styler programmer's guide"
//        2) Invoke the Shared Library through File->Execute->NX Open menu.
//
//------------------------------------------------------------------------------
extern "C" DllExport void  ufusr(char *param, int *retcod, int param_len)
{
    WCKZTest *theWCKZTest = NULL;
    try
    {
        theWCKZTest = new WCKZTest();
        // The following method shows the dialog immediately
        theWCKZTest->Show();
    }
    catch(exception& ex)
    {
        //---- Enter your exception handling code here -----
        WCKZTest::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
    }
    if(theWCKZTest != NULL)
    {
        delete theWCKZTest;
        theWCKZTest = NULL;
    }
}

//------------------------------------------------------------------------------
// This method specifies how a shared image is unloaded from memory
// within NX. This method gives you the capability to unload an
// internal NX Open application or user  exit from NX. Specify any
// one of the three constants as a return value to determine the type
// of unload to perform:
//
//
//    Immediately : unload the library as soon as the automation program has completed
//    Explicitly  : unload the library from the "Unload Shared Image" dialog
//    AtTermination : unload the library when the NX session terminates
//
//
// NOTE:  A program which associates NX Open applications with the menubar
// MUST NOT use this option since it will UNLOAD your NX Open application image
// from the menubar.
//------------------------------------------------------------------------------
extern "C" DllExport int ufusr_ask_unload()
{
    //return (int)Session::LibraryUnloadOptionExplicitly;
    return (int)Session::LibraryUnloadOptionImmediately;
    //return (int)Session::LibraryUnloadOptionAtTermination;
}

//------------------------------------------------------------------------------
// Following method cleanup any housekeeping chores that may be needed.
// This method is automatically called by NX.
//------------------------------------------------------------------------------
extern "C" DllExport void ufusr_cleanup(void)
{
    try
    {
        //---- Enter your callback code here -----
    }
    catch(exception& ex)
    {
        //---- Enter your exception handling code here -----
        WCKZTest::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
    }
}

int WCKZTest::Show()
{
    try
    {
        theDialog->Show();
    }
    catch(exception& ex)
    {
        //---- Enter your exception handling code here -----
        WCKZTest::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
    }
    return 0;
}

//------------------------------------------------------------------------------
//---------------------Block UI Styler Callback Functions--------------------------
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
//Callback Name: initialize_cb
//------------------------------------------------------------------------------
void WCKZTest::initialize_cb()
{
    try
    {
        group = dynamic_cast<NXOpen::BlockStyler::Group*>(theDialog->TopBlock()->FindBlock("group"));
        group1 = dynamic_cast<NXOpen::BlockStyler::Group*>(theDialog->TopBlock()->FindBlock("group1"));
        ProcessH = dynamic_cast<NXOpen::BlockStyler::DoubleBlock*>(theDialog->TopBlock()->FindBlock("ProcessH"));
        StartH = dynamic_cast<NXOpen::BlockStyler::DoubleBlock*>(theDialog->TopBlock()->FindBlock("StartH"));
        AppointZ = dynamic_cast<NXOpen::BlockStyler::Toggle*>(theDialog->TopBlock()->FindBlock("AppointZ"));
        SelectObj = dynamic_cast<NXOpen::BlockStyler::SelectObject*>(theDialog->TopBlock()->FindBlock("SelectObj"));
        ProgramEnum = dynamic_cast<NXOpen::BlockStyler::Enumeration*>(theDialog->TopBlock()->FindBlock("ProgramEnum"));
    }
    catch(exception& ex)
    {
        //---- Enter your exception handling code here -----
        WCKZTest::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
    }
}

//------------------------------------------------------------------------------
//Callback Name: dialogShown_cb
//This callback is executed just before the dialog launch. Thus any value set 
//here will take precedence and dialog will be launched showing that value. 
//------------------------------------------------------------------------------
void WCKZTest::dialogShown_cb()
{
    try
    {
        //---- Enter your callback code here -----
    }
    catch(exception& ex)
    {
        //---- Enter your exception handling code here -----
        WCKZTest::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
    }
}

//------------------------------------------------------------------------------
//Callback Name: apply_cb
//------------------------------------------------------------------------------
int WCKZTest::apply_cb()
{
    int errorCode = 0;
    try
    {
        //int module_id = 0;
        //UF_ask_application_module(&module_id);
        //if (module_id != UF_APP_CAM)
        //    //uc1601("请切换到加工模块", 1);
        //    theSession->ApplicationSwitchImmediate("UG_APP_MANUFACTURING");

        NXOpen::Session* theSession = NXOpen::Session::GetSession();
        NXOpen::Part* workPart(theSession->Parts()->Work());
        NXOpen::Part* displayPart(theSession->Parts()->Display());

        UF_SETUP_create(UF_ASSEM_ask_work_part(), "mill_contour");
        tag_t setup_tag = NULL_TAG;
        UF_SETUP_ask_setup(&setup_tag);
        if (setup_tag == NULL_TAG)
            uc1601("提示:请先初始化加工环境!", 1);

        tag_t geom_group = NULL_TAG;
        UF_SETUP_ask_geom_root(setup_tag, &geom_group);

        //
        char* TextString[] = { "I Need API" };
        double Origin3d[3] = { 0,0,0 };
        tag_t NoteTag = NULL_TAG;
        UF_DRF_create_note(1, TextString, Origin3d,0,&NoteTag);

        tag_t mcsTag = NULL_TAG;
        tag_t millTextTag = NULL_TAG;

        //MCS MILL_TEXT
        tag_t operMCSTag = NULL_TAG;
        char kz_mcs_str[] = "DX_KZ_MCS";
        tag_t operMillTextTag = NULL_TAG;

        UF_NCGROUP_ask_object_of_name(geom_group, kz_mcs_str, &operMCSTag);

        //Create And ReName
        if (operMCSTag == NULL_TAG)
        {
            UF_NCGEOM_create("mill_contour", "MCS", &mcsTag);
            UF_NCGROUP_accept_member(geom_group, mcsTag);
            UF_OBJ_set_name(mcsTag, kz_mcs_str);
        }


        NXOpen::CAM::OrientGeometry* orientGeometry1(dynamic_cast<NXOpen::CAM::OrientGeometry*>
            (workPart->CAMSetup()->CAMGroupCollection()->FindObject(kz_mcs_str)));
        NXOpen::CAM::NCGroup* nCGroup1;
        nCGroup1 = workPart->CAMSetup()->CAMGroupCollection()->CreateGeometry(orientGeometry1, "mill_contour", "MILL_TEXT", 
            NXOpen::CAM::NCGroupCollection::UseDefaultNameFalse, "LANZAI_MILL_TEXT");

        tag_t prog_group = NULL_TAG;

        UF_SETUP_ask_program_root(setup_tag, &prog_group);
        tag_t Program_roup = NULL_TAG;
        
        UF_NCGROUP_ask_object_of_name(prog_group, "PROGRAM", &Program_roup);//获取指定程序组的标记

        if (Program_roup == NULL_TAG)
            uc1601("没有找到PROGRAM!", 1);

        //创建操作
        tag_t operTag = NULL_TAG;
        UF_OPER_create("mill_contour", "ZLEVEL_PROFILE", &operTag);
        //添加工序操作到程序组
        UF_NCGROUP_accept_member(Program_roup, operTag);
        //修改加工坐标系的名称
        UF_OBJ_set_name(operTag, "DX_nihao");
        //创建新刀具,获取新刀具的Tag
        tag_t  NewToolObject = null_tag;
        UF_CUTTER_create("mill_contour", "MILL", &NewToolObject);
        //设定刀具名称
        const char* ToolName = "KK倒角刀";
        UF_OBJ_set_name(NewToolObject, ToolName);

        //以下设置刀具的直径、总长、锥度、刀具号、旋转方向、刀柄等参数
        UF_PARAM_set_double_value(NewToolObject, UF_PARAM_TL_DIAMETER, 5.2);//直径
        UF_PARAM_set_double_value(NewToolObject, UF_PARAM_TL_HEIGHT, 17.0);//总长
        UF_PARAM_set_double_value(NewToolObject, UF_PARAM_TL_FLUTE_LN, 21.0);//刃长
        double InOutValue[2] = { 0.005,0.005 };
        UF_PARAM_set_2d_value(NewToolObject, UF_PARAM_TOL_IN_OUT, InOutValue);//设置内、外公差

        //double PH = ProcessH->Value();
        tag_t obj_tag = SelectObj->Tag();
        ///*ProgramEnum
        //NXString enumStr = ProgramEnum->ValueAsString();*/

        //int beSelectIndex = ProgramEnum->NumberOfColumns();
        //---- Enter your callback code here -----

        UF_PARAM_set_tag_value(operTag, UF_PARAM_STOCK_PART, obj_tag);
        UF_CAMGEOM_append_items(operTag, UF_CAM_part, 1, &obj_tag, NULL);//设置指定部件(可以设置)
        UF_PARAM_set_double_value(operTag, UF_PARAM_MERGE_DIST, 3.71);//设置合并距离
        UF_PARAM_set_double_value(operTag, UF_PARAM_CUT_MIN_LENGTH, 11.1);//最小切削长度
        UF_PARAM_set_double_value(operTag, UF_PARAM_CUTLEV_MAX_DEPTH, 2.26);
        UF_PARAM_set_double_value(operTag, UF_PARAM_CUTCOM_DIST, 2.28);
        UF_CUT_LEVELS_t* cut_levels_ptr_addr;
        
        //UF_CUT_LEVELS_load(operTag, &cut_levels_ptr_addr);//获取指定切削层数据
        //UF_CUT_LEVELS_set_range_type(operTag, UF_PARAM_clv_range_user_defined, NULL);//设置切削层范围类型
        
        //char msg[256];
        //sprintf(msg, "%d", cut_levels_ptr_addr.);
        //UF_UI_write_listing_window(msg);
        tag_t ToolRootGroup = null_tag;
        UF_SETUP_ask_mct_root(setup_tag, &ToolRootGroup);
        UF_NCGROUP_accept_member(ToolRootGroup, NewToolObject);
        //添加工序操作的对应参数
        //将刀路程序
        //添加到操作中
        UF_NCGROUP_accept_member(NewToolObject, operTag);
        /*UF_NCGROUP_accept_member(programGroupName, operTag);*/
        UF_UI_ONT_refresh();//刷新加工导航器
        //UF_CALL(UF_terminate());

    }
    catch(exception& ex)
    {
        //---- Enter your exception handling code here -----
        errorCode = 1;
        WCKZTest::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
    }
    return errorCode;
}

//------------------------------------------------------------------------------
//Callback Name: update_cb
//------------------------------------------------------------------------------
int WCKZTest::update_cb(NXOpen::BlockStyler::UIBlock* block)
{
    try
    {
        if(block == ProcessH)
        {
        //---------Enter your code here-----------
        }
        else if(block == StartH)
        {
        //---------Enter your code here-----------
        }
        else if(block == AppointZ)
        {
        //---------Enter your code here-----------
        }
        else if(block == SelectObj)
        {
        //---------Enter your code here-----------
        }
        else if(block == ProgramEnum)
        {
        //---------Enter your code here-----------
        }
    }
    catch(exception& ex)
    {
        //---- Enter your exception handling code here -----
        WCKZTest::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
    }
    return 0;
}

//------------------------------------------------------------------------------
//Callback Name: ok_cb
//------------------------------------------------------------------------------
int WCKZTest::ok_cb()
{
    int errorCode = 0;
    try
    {
        errorCode = apply_cb();
    }
    catch(exception& ex)
    {
        //---- Enter your exception handling code here -----
        errorCode = 1;
        WCKZTest::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
    }
    return errorCode;
}

//------------------------------------------------------------------------------
//Function Name: GetBlockProperties
//Description: Returns the propertylist of the specified BlockID
//------------------------------------------------------------------------------
PropertyList* WCKZTest::GetBlockProperties(const char *blockID)
{
    return theDialog->GetBlockProperties(blockID);
}
